package com.chaychan.bottombarlayout_ohos.slice;

import com.chaychan.bottombarlayout_ohos.HiLogUtils;
import com.chaychan.bottombarlayout_ohos.ResourceTable;
import com.chaychan.library.BottomBarItem;
import com.chaychan.library.BottomBarLayout;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.app.dispatcher.task.TaskPriority;

import java.util.ArrayList;
import java.util.List;

public class FragmentManagerActivity extends AbilitySlice {

    private List<TabFragment> mFragmentList = new ArrayList<>();
    private BottomBarLayout mBottomBarLayout;
    private AbilitySlice abilitySlice;

    private AnimatorProperty mRotateAnimation;


    public FragmentManagerActivity(AbilitySlice abilitySlice) {
        this.abilitySlice = abilitySlice;
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_fragment_manager);

        initView();
        initData();
        initListener();

    }

    private void initView() {
        mBottomBarLayout = (BottomBarLayout) findComponentById(ResourceTable.Id_bbl);

        Image image = (Image) findComponentById(ResourceTable.Id_menuImg);
        image.setClickedListener(component -> {
            MenuDialog menuDialog = new MenuDialog(getContext());
            menuDialog.setonMenuSelectedListener(new MenuDialog.OnMenuSelectedListener() {
                @Override
                public void onMenuSelected(int position) {

                    switch (position) {
                        case 0:
                            mBottomBarLayout.setUnread(0, 0);
                            mBottomBarLayout.setUnread(1, 0);
                            break;
                        case 1:
                            mBottomBarLayout.hideNotify(2);
                            break;
                        case 2:
                            mBottomBarLayout.hideMsg(3);
                            break;
                    }
                }
            });
            menuDialog.show();
        });
    }

    private void initData() {

        TabFragment homeFragment = new TabFragment(abilitySlice, "首页");
        mFragmentList.add(homeFragment);

        TabFragment videoFragment = new TabFragment(abilitySlice, "视频");
        mFragmentList.add(videoFragment);

        TabFragment microFragment = new TabFragment(abilitySlice, "微头条");
        mFragmentList.add(microFragment);

        TabFragment meFragment = new TabFragment(abilitySlice, "我的");
        mFragmentList.add(meFragment);

        changeFragment(0); //默认显示第一页
    }


    private void changeFragment(int currentPosition) {
        ((FractionAbility) getAbility()).getFractionManager()
                .startFractionScheduler()
                .replace(ResourceTable.Id_stackLayout, mFragmentList.get(currentPosition))
                .submit();
    }

    public void initListener() {
        mBottomBarLayout.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
            @Override
            public void onItemSelected(final BottomBarItem bottomBarItem, int previousPosition, final int currentPosition) {
                HiLogUtils.error("MainActivity", "position: " + currentPosition);

                changeFragment(currentPosition);

                if (currentPosition == 0) {
                    //如果是第一个，即首页
                    if (previousPosition == currentPosition) {
                        //如果是在原来位置上点击,更换首页图标并播放旋转动画
                        if (mRotateAnimation != null && mRotateAnimation.isRunning()) {
                            //如果当前动画正在执行
                            return;
                        }


                        bottomBarItem.setSelectedIcon(ResourceTable.Media_tab_loading);//更换成加载图标

                        Image bottomImageView = bottomBarItem.getImageView();

                        //播放旋转动画
                        if (mRotateAnimation == null) {
                            mRotateAnimation = bottomImageView.createAnimatorProperty();
                            mRotateAnimation.rotate(360);
                            mRotateAnimation.setDuration(800);
                            mRotateAnimation.setLoopedCount(-1);
                        }
                        mRotateAnimation.start();//播放旋转动画


                        //模拟数据刷新完毕
                        getGlobalTaskDispatcher(TaskPriority.DEFAULT).delayDispatch(new Runnable() {
                            @Override
                            public void run() {
                                getMainTaskDispatcher().syncDispatch(new Runnable() {
                                    @Override
                                    public void run() {
                                        boolean tabNotChanged = mBottomBarLayout.getCurrentItem() == currentPosition; //是否还停留在当前页签
                                        bottomBarItem.setSelectedIcon(ResourceTable.Media_tab_home_selected);//更换成首页原来选中图标
                                        //停止旋转动画
                                        if (mRotateAnimation != null) {
                                            mRotateAnimation.cancel();
                                        }
                                    }
                                });
                            }
                        }, 3000);

                        return;
                    }
                }

                //如果点击了其他条目
                BottomBarItem bottomItem = mBottomBarLayout.getBottomItem(0);
                bottomItem.setSelectedIcon(ResourceTable.Media_tab_home_selected);//更换为原来的图标

                //停止旋转动画
                if (mRotateAnimation != null) {
                    mRotateAnimation.cancel();
                }
            }
        });


        getGlobalTaskDispatcher(TaskPriority.DEFAULT).delayDispatch(new Runnable() {
            @Override
            public void run() {
                getMainTaskDispatcher().syncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        mBottomBarLayout.setUnread(0, 20);//设置第一个页签的未读数为20
                        mBottomBarLayout.setUnread(1, 1001);//设置第二个页签的未读数
                        mBottomBarLayout.showNotify(2);//设置第三个页签显示提示的小红点
                        mBottomBarLayout.setMsg(3, "NEW");//设置第四个页签显示NEW提示文字
                    }
                });
            }
        }, 500);

    }
}
